layui.use(['table', 'jquery'], function () {
    const table = layui.table,
        $ = layui.jquery;

    showTime('#startDate');
    showTime('#endDate');
    let userCode = localStorage.getItem("loginUserCode");

    findAll();

    function findAll() {

        let chinese = $('#chinese').val();
        let english = $('#english').val();
        let author = $('#author').val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            chinese: chinese,
            english: english,
            author: author
        };

        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'chinese', width: '15%', title: '中文'}
            , {field: 'english', width: '15%', title: '英文'}
            , {field: 'author', width: '15%', title: '作者'}
            , {field: 'commitTime', width: '15%', title: '提交时间', sort: true}
            , {field: 'number', width: '10%', title: '点赞数量', sort: true}
            , {
                field: 'praiseStatus', width: '10%', align: 'center', title: '点赞',fixed: 'right', templet: e => {
                    return getPraiseStatus(e);
                }
            }
            , {
                field: 'collectionStatus', width: '10%', align: 'center', title: '收藏', fixed: 'right', templet: e => {
                    return getCollectionStatus(e);
                }
            }
        ]];

        //发送请求
        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/word/findUserWord?type=2&userCode=" + userCode
            , where: params
            , method: 'get'
            , headers: HEADERS
            , request: REQUEST
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 15, 30]
            , limit: 20
            , title: '单词数据表'
            , cols: cols
            , done: function (res, curr, count) {
                toggleStatus(res, curr, count);
            }
        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data;
        switch (obj.event) {
            case 'exportExcel':
                exportExcel(data);
                break;
        }
    });

    /**
     * 导出Excel
     */
    function exportExcel(data){
        let result = commonAjax('/word/exportExcel',JSON.stringify(data),'post');
        if (result===null){
            layerMsgOperatingSuccess();
            findAll();
        } else {
            layerMsgOperatingFail();
        }
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll();
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#chinese').val('');
        $('#english').val('');
        $('#author').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });

    /**
     * 点赞和收藏状态显示
     * @param reg
     */
    function toggleStatus(reg) {
        for (let i = 0; i < reg.data.length; i++) {
            //点赞
            $(".praiseStatus" + reg.data[i].id).bind('click', function () {
                userPraise(reg.data[i].id, reg.data[i].praiseStatus);
            });
            //收藏
            $(".collectionStatus" + reg.data[i].id).bind('click', function () {
                userCollections(reg.data[i].id, reg.data[i].collectionStatus);
            });
        }
    }

    /**
     * 用户点赞
     * @param id
     * @param praiseStatus
     */
    function userPraise(id, praiseStatus) {
        let data = {
            wordId: id,
            userCode: userCode,
        };
        let result = commonAjax('/word/userPraise?praiseStatus=' + praiseStatus, JSON.stringify(data), 'post');
        if (result.code===200){
            findAll();
        }
    }

    /**
     * 用户收藏
     * @param id
     * @param collectionStatus
     */
    function userCollections(id, collectionStatus) {
        let data = {
            wordId: id,
            userCode: userCode
        };
        let result = commonAjax('/word/userCollections?collectionStatus=' + collectionStatus, JSON.stringify(data), 'post');
        if (result.code===200){
            findAll();
        }
    }
});